package com.app.fragmentmodule

import android.os.Bundle
import android.util.Log
import com.app.common.activity.BaseActivity
import kotlinx.android.synthetic.main.activity_fragment_module.*
import androidx.core.content.ContextCompat.getSystemService
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentTransaction
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import dagger.android.AndroidInjection
import dagger.android.AndroidInjector


/**
 *@创建人 周文东
 *@创建时间 2019/11/9
 *@描述
 */
@Route(path = "/fragment_module/fragment_module_activity")
class FragmentModuleActivity : BaseActivity() {

    private val TAG = FragmentModuleActivity::class.java.simpleName

    private var fragmentOne = ARouter.getInstance().build("/fragment_module/fragment_one").navigation() as Fragment

    private val fragmentTwo = ARouter.getInstance().build("/fragment_module/fragment_two").navigation() as Fragment

    private var fragmentThree = ARouter.getInstance().build("/fragment_module/fragment_three").navigation() as Fragment

    private val fragmentFour = ARouter.getInstance().build("/fragment_module/fragment_four").navigation() as Fragment

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //AndroidInjection.inject(this)
        setContentView(R.layout.activity_fragment_module)

        initFragment()

        activity_fragment_module_bnv.setOnNavigationItemSelectedListener {
            when (it.itemId) {
                R.id.tab_one -> showFragment(fragmentOne)
                R.id.tab_two -> showFragment(fragmentTwo)
                R.id.tab_three -> showFragment(fragmentThree)
                R.id.tab_four -> showFragment(fragmentFour)
            }
            
            false
        }

    }

    fun initFragment() {

        val fragmentTransaction : FragmentTransaction = supportFragmentManager.beginTransaction()

        fragmentTransaction.add( R.id.activity_fragment_module_container, fragmentOne)
        fragmentTransaction.add( R.id.activity_fragment_module_container, fragmentTwo)
        fragmentTransaction.add( R.id.activity_fragment_module_container, fragmentThree)
        fragmentTransaction.add( R.id.activity_fragment_module_container, fragmentFour)
        fragmentTransaction.hide(fragmentTwo)
        fragmentTransaction.hide(fragmentThree)
        fragmentTransaction.hide(fragmentFour)
        fragmentTransaction.show(fragmentOne)
        fragmentTransaction.commit()

    }

    fun showFragment(fragment: Fragment) {

        val fragmentTransaction : FragmentTransaction = supportFragmentManager.beginTransaction()

        fragmentTransaction.hide(fragmentOne)
        fragmentTransaction.hide(fragmentTwo)
        fragmentTransaction.hide(fragmentThree)
        fragmentTransaction.hide(fragmentFour)

        if (!fragment.isVisible) {
            fragmentTransaction.show(fragment)
        }
        fragmentTransaction.commit()

    }

}